import {ObtainLoginData} from "./ObtainLoginData.js";
import {DataStore} from "./DataStore.js";
import {CheckSession} from "./CheckSession.js";
import {DeterMineData} from "./DeterMineData.js";
import {USERLOGIN} from "./ConstUrl.js";

export class UserLogin extends DeterMineData {
    constructor(game_id, game_auth_code) {
        super();
        this.cheSession = new CheckSession();
        this.obtainLoginData = new ObtainLoginData();
        this.game_id = game_id;
        this.game_auth_code = game_auth_code;
    }


    userLogin(successCallback = super.request, failsCallback = super.request) {
        this.cheSession.checkSession(res => {
            this.deterMineData(successCallback, failsCallback);
        }, res => {
            this.obtainLoginData.getData(res => {
                this.deterMineData(successCallback, failsCallback);
            }, res => {
                failsCallback(res);
            })
        })
    }

    deterMineData(successCallback, failsCallback) {
        super.deterMineData(failsCallback, () => {
            this.loginServer(successCallback, failsCallback);
        })
    }

    loginServer(successCallback, failsCallback) {
        const userinfo = this.dataStore.get('userInfo');
        const systemInfo = this.dataStore.get('systemInfo');
        const networkType = this.dataStore.get('networkType');

        const params = {
            open_id: userinfo.open_id,
            iv: userinfo.iv,
            encryptedData: userinfo.encryptedData,
            rawData: userinfo.rawData,
            signature: userinfo.signature,
            brand: systemInfo.brand,
            model: systemInfo.model,
            version: systemInfo.version,
            system: systemInfo.system,
            platform: systemInfo.platform,
            network: networkType.networkType,
        };

        super.requestServer(successCallback, failsCallback, USERLOGIN, params)
    }
}